package com.hsbc.dev.teamo4.sms.sm.dao.impl;

import com.hsbc.dev.teamo4.sms.common.utils.factory.DBConnectFactory;
import com.hsbc.dev.teamo4.sms.common.utils.factory.UUIDFactory;
import com.hsbc.dev.teamo4.sms.sm.dao.ILogDao;
import com.hsbc.dev.teamo4.sms.sm.po.Log;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @Description: 
 * @author:Young
 * @version:1.0
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.sm.dao.impl
 * @Date date: 2018/7/26
 */
public class LogDaoImpl implements ILogDao {

    private QueryRunner queryRunner = DBConnectFactory.getQueryRunner();

    /**
     * @Description: 插入一条日志记录。
     * @Param: [log]
     * @return: com.hsbc.dev.teamo4.sms.sm.po.Log
     * @Author: Young
     * @Date: 2018/7/29
     */
   @Override
    public Log saveLog(Log log) {
        String sql = "insert into sm_log(id,operatorTable,operatorId,operatorIp,operatorTime,operatorType,isEnable,logContent) values(?,?,?,?,?,?,?,?)";
       int row = 0;
       try {
           row = queryRunner.update(sql, UUIDFactory.getUUID(),log.getOperatorTable(),log.getOperatorId(),log.getOperatorIp(),log.getOperatorTime(),log.getOperatorType(),log.getIsEnable(),log.getLogContent());
       } catch (SQLException e) {
           e.printStackTrace();
       }
       if (row>0){
           return log;
       }
       return null;
    }

    /**
     * @Description: 查询指定数据库表的所有日志记录。
     * @Param: [tableName]
     * @return: java.util.List<com.hsbc.dev.teamo4.sms.sm.po.Log>
     * @Author: Young
     * @Date: 2018/7/29
     */
    @Override
    public List<Log> queryLogByTableName(String tableName) {
        String sql = "select id,operatorTable,operatorId,operatorIp,operatorTime,operatorType,isEnable,logContent from sm_log where operatorTable = ? and isEnable = true";
        List<Log> logs = null;
        try {
             logs = queryRunner.query(sql, new BeanListHandler<Log>(Log.class), tableName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return logs;
    }

    @Override
    public List<Log> queryLogInDay(String tableName, Date date) {

        return null;
    }
}
